import $ from 'jquery';
import constant from '@/utils/constant';
import {
	getToken
} from '@/utils/auth';

const uwLoadJs = {

	ajaxRequest: function(params, url, ansyc) {
		let data = null;
		let token = null;
		let flag = params['flag'];//如果是公共请求，则需要携带改参数
		let urlParams = '';
		
		if (flag && flag !=undefined &&flag != '') {
			if (flag == "wx") {
				token = localStorage.getItem(constant.WX_TOKEN);
			} else {
				token = localStorage.getItem(constant.YS_TOKEN);
			}
		} else {
			if (url.indexOf("wx") >= 0) {
				token = localStorage.getItem(constant.WX_TOKEN);
			} else {
				token = localStorage.getItem(constant.YS_TOKEN);
			}
		}

		$.ajax({
			headers: {
				Authorization: token
			},
			url: constant.baseUrl + url, //url路径
			type: 'POST', //GET
			async: ansyc || false, //或false,是否异步
			data: JSON.stringify(params),
			contentType: "application/json;charset=UTF-8",
			success: function(res, textStatus, jqXHR) {
				if ('40101' == res.resultCode) {
					localStorage.clear();
					//员工
					if (url.indexOf('tech') >= 0) {
						window.location.href = constant.loginUrl;
					} else {
						window.location.href = constant.userLoginUrl;
					}

				} else {
					data = res;
				}
			},
			error: function(xhr, textStatus, errorThrown) {

			}
		})
		return data;
	},

	checkSessionChange: function(params) {
		let url = appName.NEWUNDR + '/unipnd/nucleusNote/checkSessionChange';
		return uwLoadJs.ajaxRequest(params, url);
	},
}

export default uwLoadJs;
